import java.util.*;

/**
 * @author dream
 * @date 2021/1/2 22:31
 */
public class Main {
    final static int ARRAY_LENGTH = 10000;

    public static void main(String[] args) {
        int[] array = new int[ARRAY_LENGTH];
        for (int i = 0; i < ARRAY_LENGTH; i++) {
            array[i] = new Random().nextInt(10000);
        }
        System.out.println("原始数据:" + Arrays.toString(array));
        System.out.println("——————————————————————————————————————————");
        String[] sortTypeArray = new String[]{"选择", "插入", "冒泡", "快速", "归并", "希尔"};
        List<SortingResultInfo> list = new ArrayList<>(sortTypeArray.length);
        for (String s : sortTypeArray) {
            list.add(sort(s, array));
        }
        Collections.sort(list);
        System.out.println("\n运行时间对比\n");
        for (SortingResultInfo sortingResultInfo : list) {
            try {
                System.out.println(sortingResultInfo.toString());
            } catch (DuplicateFormatFlagsException e) {
                System.out.println(sortingResultInfo.getName());
            }
        }
    }

    public static SortingResultInfo sort(String nameType, int[] array) {
        SortingResultInfo sortingResultInfo = new SortingResultInfo();
        sortingResultInfo.setName(nameType + "排序");
        System.out.println(sortingResultInfo.getName());
        sortingResultInfo.setStartTime(System.currentTimeMillis());
        int[] newArray = new int[0];
        switch (nameType) {
            case "选择":
                newArray = Sort.selectionsort(array.clone());
                break;
            case "插入":
                newArray = Sort.insertionSorting(array.clone());
                break;
            case "冒泡":
                newArray = Sort.bubbleSort(array.clone());
                break;
            case "快速":
                newArray = Sort.quickSort(array.clone());
                break;
            case "归并":
                newArray = Sort.mergeSort(array.clone());
                break;
            case "希尔":
                newArray = Sort.shellSort(array.clone());
                break;
            default:
                break;
        }
        sortingResultInfo.setEndTime(System.currentTimeMillis());
        System.out.println(sortingResultInfo.getName() + "时间：" + sortingResultInfo.getWhen() + "ms");
        System.out.println(Arrays.toString(newArray));
        System.out.println("——————————————————————————————————————————————————————————————");
        return sortingResultInfo;
    }
}